package com.netforce.web.admin.mvc;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.client.utils.URIBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.netforce.core.report.Reports;
import com.netforce.data.dto.AvailableSObjectsDto;
import com.netforce.data.dto.ResourcesDto;
import com.netforce.data.dto.SOQLQueryResultDto;
import com.netforce.data.dto.SObjectDto;
import com.netforce.data.dto.VersionDefinitionDto;
import com.netforce.data.source.Connection;
import com.netforce.data.source.DataSource;
import com.netforce.util.io.IoManager;

public interface TestController
{
  @RequestMapping(value = "/testMysql.do", method = RequestMethod.GET)
  public String doTestMysql(HttpServletRequest aRequest, HttpServletResponse aResponse) throws Exception;

  @RequestMapping(value = "/testRestApi.do", method = RequestMethod.GET)
  public String doTestRestApi(HttpServletRequest aRequest, HttpServletResponse aResponse) throws Exception;

  @Controller
  public class DefaultTestController implements TestController
  {
    @Override
    public String doTestMysql(HttpServletRequest aRequest, HttpServletResponse aResponse) throws Exception
    {
      Class.forName("com.mysql.jdbc.Driver");
      java.sql.Connection connect = DriverManager.getConnection("jdbc:mysql://localhost/wordpress", "romain", "romain");
      Statement statement = connect.createStatement();
      ResultSet resultSet = statement.executeQuery("select * from test");
      while (resultSet.next())
      {
        System.out.println("idtest=" + resultSet.getInt("idtest"));
      }
      return "/";
    }

    @Override
    public String doTestRestApi(HttpServletRequest aRequest, HttpServletResponse aResponse) throws Exception
    {
      Reports.info("test");
      Connection connection = (Connection)aRequest.getSession().getAttribute("connection");

      Object object = null;

      object = connection.httpGet(VersionDefinitionDto[].class, "/services/data");
      object = connection.httpGet(ResourcesDto.class, "/services/data/v30.0");
      object = connection.httpGet(AvailableSObjectsDto.class, "/services/data/v30.0/sobjects");
      object = connection.httpGet(SObjectDto.class, "/services/data/v30.0/sobjects/Account/describe");
      object = connection.httpGet(SOQLQueryResultDto.class, new URIBuilder("/services/data/v30.0/query").addParameter("q", "Select name from Account").toString());

      object = IoManager.toString(connection.httpGet(new URIBuilder("/services/data/v30.0/chatter/users/me").toString()));
      return "/";
    }

    @Autowired
    protected DataSource DataSource;
    @Autowired
    protected Reports Reports;
    @Autowired
    protected IoManager IoManager;
  }
}
